package top.yuzt.animalserver.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.yuzt.animalserver.entity.Animal;
import top.yuzt.animalserver.entity.Feed;
import top.yuzt.animalserver.entity.PackFeed;
import top.yuzt.animalserver.service.impl.AnimalServiceImpl;
import top.yuzt.animalserver.service.impl.FeedServiceImpl;
import top.yuzt.animalserver.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuzt
 * @since 2024-05-17
 */
@RestController
    @RequestMapping("/feed")
public class FeedController {

    @Autowired
    private FeedServiceImpl feedService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private AnimalServiceImpl animalService;

    @SaIgnore
    @GetMapping("/getlist")
    public List<PackFeed> getuserlist(){
        List<PackFeed> packFeedList = new ArrayList<>();
        for (Feed x : feedService.list()){
            Animal animal = animalService.getById(x.getAnimalId());
            if(animal != null){
                String keepername = userService.getById(x.getKeeperId()).getNickname();
                packFeedList.add(new PackFeed(x,animal,keepername));
            }
        }
        return packFeedList;
    }
    @SaIgnore
    @GetMapping("/getlist/byuserid")
    public List<PackFeed> getuserlistByID(Integer id){
        if(Integer.parseInt(userService.getById(id).getRole()) > 2){
            return getuserlist();
        }else {
            List<PackFeed> packFeedList = new ArrayList<>();
            for (Feed x : feedService.list()){
                Animal animal = animalService.getById(x.getAnimalId());
                if(animal != null){
                    if(x.getKeeperId() == id){
                        String keepername = userService.getById(x.getKeeperId()).getNickname();
                        packFeedList.add(new PackFeed(x,animal,keepername));
                    }
                }
            }
            return packFeedList;
        }
    }

    //新增
    @SaIgnore
    @PostMapping("/save")
    public boolean savefeed(@RequestBody Feed feed){
        Animal animal =  animalService.getById(feed.getAnimalId());
        Double tmp = randomNumber(feed.getFoodWeigh(),animal.getNowWeight());
        animal.setNowWeight(tmp);
        if(tmp > 100){
            animal.setHealthStatus("3");
        }else {
            animal.setHealthStatus("1");
        }
        animalService.updateById(animal);
        feed.setAnimalAddWeigh(tmp);
        return feedService.save(feed);
    }
    //更新
    @SaIgnore
    @PostMapping("/update")
    public boolean updatefeed(@RequestBody Feed feed){
        return  feedService.updateById(feed);
    }
    //删除
    @SaIgnore
    @PostMapping("/remove")
    public boolean removefeed(@RequestBody Feed feed){
        return  feedService.removeById(feed.getId());
    }

    private static Double randomNumber(double num,double weight) {
        Random random = new Random();
        double tmp = num;
        double x = random.nextInt(10);
        tmp = tmp*x*0.08;
        Double ans = 0d;
        if(x%10 < 3){
             ans = weight+tmp;
        }else {
             ans = weight-x;
            if(ans < 0){
                ans = weight+tmp;
            }
        }
        ans = Math.round(ans * 100.0) / 100.0;
        return ans;
    }
}
